﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;
using MQ.Common;
using RabbitMQ.Client.Events;

namespace MQ.Consumer
{
    public class Receive_Normal
    {
        public static void Go()
        {
            string queueName = "normal";
            var conn = RabbitMQHelper.GetConnection();
            {
                var channel = conn.CreateModel();
                {
                    //定义队列
                    channel.QueueDeclare(queueName,false,false,false,null);
                    //定义消费者
                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += Consumer_Received;//给消费者接收消息的事件
                    channel.BasicConsume(queueName, true, consumer);//绑定消费者到队列
                }
            }
        }

        private static void Consumer_Received(object? sender, BasicDeliverEventArgs e)
        {
            var msg = Encoding.UTF8.GetString(e.Body.ToArray());
            Console.WriteLine($"接收到消息：{msg}");
        }
    }
}
